//
//  UserListViewController.m
//  豆瓣_精简版
//
//  Created by dllo on 15/11/6.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "UserListViewController.h"
#import "LoginViewController.h"
#import "DataBase.h"
#import "UserDetailViewController.h"
@interface UserListViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,retain)UITableView *myTableView;

@end

@implementation UserListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"我的";
    
    NSString *string = [[NSUserDefaults standardUserDefaults]objectForKey:@"state"];
    NSString *name = @"";
    if ([string isEqualToString:@"1"]) {
        name = @"注销";
    }else{
        name = @"登陆";
    }
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:name style:UIBarButtonItemStyleDone target:self action:@selector(rightAction:)];
    
    self.myTableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    
    
    [[DataBase shareDataBase]openDB];
    [[DataBase shareDataBase]createUserTable];
    
}

-(void)viewWillAppear:(BOOL)animated{
    
    
    NSString *string = [[NSUserDefaults standardUserDefaults]objectForKey:@"state"];
    if ([string isEqualToString:@"1"]) {
        self.navigationItem.rightBarButtonItem.title = @"注销";
    }else{
        self.navigationItem.rightBarButtonItem.title = @"登陆";
    }
    
}

- (void)rightAction:(UIBarButtonItem *)bar{
    
    NSString *string = [[NSUserDefaults standardUserDefaults]objectForKey:@"state"];
    if ([string isEqualToString:@"1"]) {
        [[NSUserDefaults standardUserDefaults]setObject:@"0" forKey:@"state"];
        self.navigationItem.rightBarButtonItem.title = @"登陆";
        
    }else{
        LoginViewController *loginVC = [[LoginViewController alloc]init];
        UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:loginVC];
        [self presentViewController:naVC animated:YES completion:^{
            
            
        }];
    }
    
    
}



//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 13;
}

//每行内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    if (indexPath.row == 0) {
        cell.textLabel.text = @"我的电影";
    }else if(indexPath.row == 1){
        cell.textLabel.text = @"清除缓存";
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UserDetailViewController *userDVC = [[UserDetailViewController alloc]init];
    NSString *string = [[NSUserDefaults standardUserDefaults]objectForKey:@"state"];
    if ([string isEqualToString:@"1"]) {
        if (indexPath.row == 0) {
        [self.navigationController pushViewController:userDVC animated:YES];
        }
    }else{
        LoginViewController *loginVC = [[LoginViewController alloc]init];
        UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:loginVC];
        [self presentViewController:naVC animated:YES completion:^{
            
            
        }];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
